#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A simple but smart HTTP client. Give extra information about HTTP redirect
so you can catch the original URL
"""

import socket
import urllib2 
import time

HTTP_TIMEOUT =  5 #seconds
USER_AGENT =  'Sushi_crawler/0.1'

socket.setdefaulttimeout(HTTP_TIMEOUT)

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    """
    Defining better handler for 301/302 redirect
    """

    def http_error_301(self, req, fp, code, msg, headers):  
        result = urllib2.HTTPRedirectHandler.http_error_301( 
            self, req, fp, code, msg, headers)              
        result.redirect = code                                 
        return result                                       

    def http_error_302(self, req, fp, code, msg, headers):   
        result = urllib2.HTTPRedirectHandler.http_error_302(
            self, req, fp, code, msg, headers)              
        result.redirect = code                                
        return result                               


class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler):
    """
    Defining better handler for HTTP errors
    """

    def http_error_default(self, req, fp, code, msg, headers): 
        result = urllib2.HTTPError(                           
            req.get_full_url(), code, msg, headers, fp)       
        result.code = code                                   
        return result              

opener = urllib2.build_opener(DefaultErrorHandler(),SmartRedirectHandler())

def get(url):
    """
    returns a file-like object with additional attributes:
    - code
    - redirect
    so you can catch the original URL that lead to a redirect.

    In case of network error return an URLError    
    """
    req = urllib2.Request(url)
    req.add_header('User-Agent', USER_AGENT)
    req.add_header('Accept-encoding', 'gzip')
    try:
        response = opener.open(req)
    except urllib2.URLError, e:
        response = e
    return response
